package com.aier.classes.action;

import java.io.File;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.aier.classes.entity.Classes;
import com.aier.classes.service.IClassService;
import com.aier.common.common.Constant;
import com.aier.common.common.ImageUtil;
import com.aier.common.common.SystemConfigStatic;
import com.aier.school.service.ISchoolService;

@Scope("prototype")
@Component("classesBaseInfoAction")
public class ClassesBaseInfoAction extends ClassesBaseAction {

    /**
     * 班级管理-班级基本信息、班徽
     */
    private static final long serialVersionUID = 1L;

    @Resource(name = "classService")
    private IClassService classService;
    @Resource(name = "schoolService")
    private ISchoolService schoolService;

    private int s; // 成功:1 ,失败:0
    private String error_info; // 错误提示信息

    private Classes classes; // 班级
    private Long cid; // 班级id
    private String cname; // 班级名称
    private String desinfo; // 班级信息
    private String manifesto; // 班级宣言
    private String img; // 班级主图
    private String clogo; // 班徽
    private File file;
    private String photoName; // 照片原名


    private int x;
    private int y;
    private int w;
    private int h;

    @Override
    public String execute() {
        classes = classService.searchClassById(cid);
        return SUCCESS;
    }

    /**
     * 修改班级信息
     * 
     * @return
     */
    public String updateClassInfo() {
        s = 1;
        Integer sid = schoolService.searchSchoolByCid(cid).getSid();
        int class_num = classService.countClassByName(cname, sid, cid);
        if (class_num > 0) {
            s = 0;
            error_info = "班级名已经存在";
        } else {
            classes = new Classes(cid, cname, null, desinfo, manifesto, img,
                    null, null);
            if (classService.updateClassInfo(classes) == 0) {
                s = 0;
                error_info = "修改班级信息失败";
            }
        }
        return SUCCESS;
    }

    /**
     * 修改班级logo(Jquery版)
     * 
     * @return
     */
    public String updateClassLogo() {
        clogo = ImageUtil.uploadImage(file,
                SystemConfigStatic.classlogo_upload_url, photoName, 0);
        s = 1;
        return SUCCESS;
    }
    
    /**
     * 修改班级logo(flash版)
     * 
     * @return
     */
    public String updateFlashClassLogo() {
        clogo = ImageUtil.uploadImageAdjustable(file, classlogo_upload_url, photoName,  Constant.num_100, 
                Constant.num_100, Constant.num_200, Constant.num_200, true);
        if (classService.updateClassLogo(cid, clogo) == 0) {
            s = 0;
            error_info = "修改班级logo失败";
        }
        s=1;
        return SUCCESS;
    }

    // 剪切班徽
    public String cutImg() {
        
        s = ImageUtil.cutImg(SystemConfigStatic.classlogo_upload_url, clogo, x, y, w, h);
        if (classService.updateClassLogo(cid, clogo) == 0) {
            s = 0;
            error_info = "修改班级logo失败";
        }

        return SUCCESS;
    }
    
    // 读取班徽
    public String serachAvatar() {
        Classes c = classService.searchClassById(cid);
        if (null != c) {
            clogo = c.getClogo();
        }
        return SUCCESS;
    }

    public int getS() {

        return s;
    }

    public String getError_info() {
        return error_info;
    }

    public Classes getClasses() {
        return classes;
    }

    public void setClassService(IClassService classService) {
        this.classService = classService;
    }

    public void setCid(Long cid) {
        this.cid = cid;
    }

    public void setCname(String cname) {
        this.cname = cname;
    }

    public void setDesinfo(String desinfo) {
        this.desinfo = desinfo;
    }

    public void setManifesto(String manifesto) {
        this.manifesto = manifesto;
    }

    public void setImg(String img) {
        this.img = img;
    }

    public void setClogo(String clogo) {
        this.clogo = clogo;
    }

    public Long getCid() {
        return cid;
    }

    public String getClogo() {
        return clogo;
    }



    public void setPhotoName(String photoName) {
        this.photoName = photoName;
    }

    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }

    public int getW() {
        return w;
    }

    public void setW(int w) {
        this.w = w;
    }

    public int getH() {
        return h;
    }

    public void setH(int h) {
        this.h = h;
    }

    public void setFile(File file) {
        this.file = file;
    }
}
